package com.sirap.fish.fetchers;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.tool.C;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.Colls;
import com.sirap.basic.util.MathUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.common.domain.Album;
import com.sirap.extractor.images.CatFetcher;

public class CatImageFetchers {
	
	private static final int TOP_COUNT = 6;

	public static void main(String[] args) {
		Object va = null;
		String vb = null;
//		va = getAlbums(3);
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(1);
//		va = getAlbumsByTypeUrl("https://www.cct58.com/mneinv/19466/");
		
//		va = getAlbumsByPersonUrl("https://www.cct58.com/xiezhen/8608/mx27/", null);
//		va = getAlbumsByPersonUrl("https://www.cct58.com/mneinv/6953/mx27/", null);
		va = getAlbum("https://www.cct58.com/mneinv/6953/mx27id16574pg0.html");
//		va = lineToMap("19447  mneinv 夏雨桐             ");
//		vb = "http://www.699mm.com/zhubo/86290_15.html";
//		vb = "http://www.699mm.com/zhubo/86290.html";
//		vb = "http://www.699mm.com/search-0-1343.html";
//		va = getAlbumsByPageUrl(vb);
//		va = imagesInPage(vb);
//		va = tagsFromList(IOUtil.readLinesWithUTF8("C:/mastorage/may/all.words.txt"));
//		D.pjsp(va, Album.class);
	}
	
	private static final Pattern PAT = Pattern.compile("(\\d+)\\s+(\\S+)\\s+(.+)");
	
	private static Map<String, Object> lineToMap(String line) {
		Matcher ma = PAT.matcher(line.trim());
		String temp = "https://www.cct58.com/{0}/{1}/mx27/";
		if(ma.find()) {
			String sid = ma.group(1).trim();
			String type = ma.group(2).trim();
			String word = ma.group(3).trim();
//			D.pla(temp, type, sid);
			Map<String, Object> item = Amaps.createMap("word", word);
			item.put("url", StrUtil.occupy(temp, type, sid));
			return item;
		}
		
		return null;
	}
	
	public static List<Map<String, Object>> tagsFromList(List<String> lines) {
		List<Map<String, Object>> tags = Lists.newArrayList();
		Collections.shuffle(lines);
		for(String line : lines) {
			Map<String, Object> item = lineToMap(line);
			if(item != null) {
				tags.add(item);
			}
		}
		
		return tags;
	}
	
	/***
	 * 获得每人对应的相册
	 * 1) https://www.cct58.com/xiezhen/8608/mx27/  使用<div class="my5_dy_1" id="my5csy5">
	 * 2) https://www.cct58.com/mneinv/6953/mx27/	使用<div class="cn">
	 * @param aurl
	 * @param linesOfNameAndId
	 * @return
	 */
	public static Map<String, Object> getAlbumsByPersonUrl(String aurl, List<String> linesOfNameAndId) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return aurl;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				int maxpage = 1;
				String r1 = "<a class=\"a1\">\\s*(\\d+)\\s*条\\s*</a>";
				String count = StrUtil.findFirstMatchedItem(r1, source);
				if(count != null) {
					D.pl(count);
					maxpage = MathUtil.totalPageOf(Integer.parseInt(count), 12);
				}
				
				item.put("totalpage", maxpage);
				
				String r3 = "<a href=\"([^\"]+)\"[^<>]*>下一页</a>";
				String next = StrUtil.findFirstMatchedItem(r3, source);
				String fullurl = StrUtil.useDelimiter("/", "https://www.cct58.com", next);
				item.put("nextpage", fullurl);
				
				String regexLis = "(my5csy5|cn)\">(.+?)\\s*<div";
				Matcher m2 = createMatcher(regexLis, source);
				String lis = "";
				if(m2.find()) {
					lis = m2.group(2);
				}
				
				String regex = "<li>(.+?)</p></a></li>";
				Matcher ma = createMatcher(regex, lis);
				while(ma.find()) {
					Map<String, Object> row = Amaps.newLinkHashMap();
					String kid = ma.group(1);
					String aurl = StrUtil.findFirstMatchedItem("<a href=\"(http[^\"]+)\"", kid);
					String cover = StrUtil.findFirstMatchedItem("<img src=\"([^\"]+)\"", kid);
					String title = StrUtil.findFirstMatchedItem("alt=\"([^\"]+)\"", kid);
					row.put("aurl", aurl);
					row.put("cover", cover);
					row.put("title", title);
					if(linesOfNameAndId != null) {
						Collections.shuffle(linesOfNameAndId);
						row.put("tags", tagsFromList(Colls.top(linesOfNameAndId, TOP_COUNT)));
					}
					
					mexItems.add(row);
				}
				
				item.put("photos", mexItems);
				C.listSome(mexItems, 3);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Map<String, Object> getAlbumsByTypeUrl(String aurl) {
		return getAlbumsByTypeUrl(aurl, null);
	}

	public static Map<String, Object> getAlbumsByTypeUrl(String aurl, List<String> linesOfNameAndId) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return aurl;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				
				String r1 = "<a class=\"a1\">\\s*(\\d+)\\s*条\\s*</a>";
				String acount = StrUtil.findFirstMatchedItem(r1, source);
				if(acount != null) {
					int maxpage = MathUtil.totalPageOf(Integer.parseInt(acount), 30);
					item.put("totalpage", maxpage);
				}
				
				//<a href="/mneinv/2.html" class="a1">下一页</a>
				String r3 = "<a href=\"([^\"]+)\"[^<>]*>下一页</a>";
				String next = StrUtil.findFirstMatchedItem(r3, source);
				String fullurl = StrUtil.useDelimiter("/", "https://www.cct58.com", next);
				item.put("nextpage", fullurl);

//				String regex = "<ul class=\"clearfix\">(.+?)</ul>";
				String regex = "<li>\\s*<div class=\"listbox\">(.+?)</div>\\s*</li>";
//				String kids = StrUtil.findFirstMatchedItem(regex, source);
				Matcher ma = createMatcher(regex, source);
				while(ma.find()) {
					Map<String, Object> row = Amaps.newLinkHashMap();
					String kid = ma.group(1);
					String aurl = StrUtil.findFirstMatchedItem("<a href=\"(http[^\"]+)\"", kid);
					String cover = StrUtil.findFirstMatchedItem("<img src=\"([^\"]+)\"", kid);
					String title = StrUtil.findFirstMatchedItem("alt=\"([^\"]+)\"", kid);
//					String when = StrUtil.findFirstMatchedItem("<b class=\"b1\">([^<>]+)</b>", kid);
					
					row.put("aurl", aurl);
					row.put("cover", cover);
					row.put("title", title);
					if(linesOfNameAndId != null) {
						row.put("tags", tagsFromList(linesOfNameAndId));
					}
					
					mexItems.add(row);
				}
				
				item.put("photos", mexItems);
				C.listSome(mexItems, 3);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Album getAlbum(String albumurl) {
		Album bum = CatFetcher.of(albumurl);
		
		return bum;
	}
	
	public static String countOfImages(String albumurl) {
		Map<String, Object> info = CatFetcher.countAndTitleOf(albumurl);
		return info.get("count") + "";
	}
}
